package com.tjcd.project.library_base.common.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/8
 * @Description 测量工具
 * @SpittingGrooves
 */
public class MeasureUtil {

    /**
     * sp转为px
     *
     * @param context
     * @param sp
     * @return
     */
    public static int sp2px(Context context, float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
    }

    /**
     * 设置margin
     *
     * @param v
     * @param l
     * @param t
     * @param r
     * @param b
     */
    public static void setMargin(View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }

    /**
     * 设置宽高
     */
    public static void setWidth(View v, int width) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) v.getLayoutParams();
            linearParams.width = width;
            v.setLayoutParams(linearParams);
        }
    }

    /**
     * 设置高度
     */
    public static void setHeight(View v, int height) {
        try {
            ConsecutiveScrollerLayout.LayoutParams linearParams = (ConsecutiveScrollerLayout.LayoutParams) v.getLayoutParams();
            linearParams.height = height;
            v.setLayoutParams(linearParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置padding
     *
     * @param v
     * @param l
     * @param t
     * @param r
     * @param b
     */
    public static void setPadding(View v, int l, int t, int r, int b) {
        v.setPadding(l, t, r, b);
    }

    /**
     * 测量单元宽度最大值
     *
     * @param titles
     * @param datas
     * @return
     */
    public static List<Integer> measureCellWidthMax(Context context, List<String> titles, List<List<String>> datas, int unit) {
        List<Integer> cellWidths = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            String title = titles.get(i);
            int dataWidth = 0;
            int maxWidth = 0;
            int maxSize = 0;
            for (int j = 0; j < datas.size(); j++) {
                if (!TextUtils.isEmpty(datas.get(j).get(i))) {
                    dataWidth = Math.max(getTextWidth(datas.get(j).get(i), context, unit), maxSize);
                    maxSize = dataWidth;
                }
            }
            maxWidth = Math.max(getTextWidth(title, context, unit), dataWidth);
            if (maxWidth > SizeUtils.dp2px(4 * 40)) { // 根据excelview最大宽度
                maxWidth = SizeUtils.px2dp(4 * 40);
            }
            cellWidths.add(maxWidth);
        }
        return cellWidths;
    }

    /**
     * 测量单元宽度最大值
     *
     * @param titles
     * @param datas
     * @return
     */
    public static int measureCellWidthMax(Context context, String titles, List<List<String>> datas, int unit) {
        return measureCellWidthMax(context, Collections.singletonList(titles), datas, unit).get(0);
    }


    /**
     * 获取文字的宽度 1中文=2字符
     *
     * @param context
     * @param unit
     * @return
     */
    public static int getTextWidth(String str, Context context, int unit) {
        if (str == null) return 0;
        int width = 0;
        for (char c : str.toCharArray()) {
            if (isChinese(c)) {
                width += 2 * sp2px(context, unit);
            } else {
                width += sp2px(context, unit);
            }
        }
        return width;
    }

    /**
     * 判断是否是中文
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断
    }

}
